//
//  AgreeView.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/15.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "AgreeView.h"

@interface AgreeView ()<TTTAttributedLabelDelegate>

@property (nonatomic,strong) UIButton *agreeBtn;

@property (nonatomic,strong) TTTAttributedLabel *label;

@property (nonatomic,assign) BOOL isAgree;

@property (nonatomic,copy) LinkBlock Selectblock;

@property (nonatomic,copy) StateChangeBlock stateBlock;

@end

@implementation AgreeView
- (instancetype)initwithText:(NSString *)textStr andLinkText:(NSString *)linkStr Selectblock:(LinkBlock)Selectblock StateChangeBlock:(StateChangeBlock)stateBlock{
    if ([super init]) {
        _isAgree = YES;
        _Selectblock = Selectblock;
        _stateBlock = stateBlock;
        [self addSubview:self.agreeBtn];
        [self addSubview:self.label];
        [_label setStlyeByText:textStr andLinkText:linkStr];
    }
    return self;
}
- (void)layoutSubviews{
    [_agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_agreeBtn.mas_right).offset(2);
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(_agreeBtn);
    }];
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    if (_Selectblock) {
        _Selectblock();
    }
}

- (UIButton *)agreeBtn{
    if (!_agreeBtn) {
        _agreeBtn = [UIButton new];
        [_agreeBtn setBackgroundImage:_isAgree?UIImageMake(@"agreeicon"):[UIImageMake(@"agreeicon") qmui_imageWithTintColor:[UIColor grayColor]] forState:UIControlStateNormal];
        [_agreeBtn bk_addEventHandler:^(id sender) {
            _isAgree = !_isAgree;
            if (_stateBlock) {
                _stateBlock(_isAgree);
            }
            [_agreeBtn setBackgroundImage:_isAgree?UIImageMake(@"agreeicon"):[UIImageMake(@"agreeicon") qmui_imageWithTintColor:[UIColor grayColor]] forState:UIControlStateNormal];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBtn;
}
- (TTTAttributedLabel *)label{
    if (!_label) {
        _label = [TTTAttributedLabel new];
  
        _label.delegate = self;
    }
    return _label;
}

@end
